﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using DataAccessLayer.DAL;
using Common;

namespace BestBuy.ManagerViews
{
    public partial class UserManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user == null || user.RoleID != (int)RoleType.Manager)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
            if (!IsPostBack)
            {
                LoadUser();
            }

        }

        private void LoadUser()
        {
            gvBuyer.DataSource = UserDAL.GetUserByRole((int)RoleType.Buyer, (int)UserStatus.Active);
            gvBuyer.DataBind();
            lbBuyerActived.Text = gvBuyer.Rows.Count.ToString();
            gvBuyerLocked.DataSource = UserDAL.GetUserByRole((int)RoleType.Buyer, (int)UserStatus.Locked);
            gvBuyerLocked.DataBind();
            lbBuyerLocked.Text = gvBuyerLocked.Rows.Count.ToString();
            gvSeller.DataSource = UserDAL.GetUserByRole((int)RoleType.Seller, (int)UserStatus.Active);
            gvSeller.DataBind();
            lbSellerActived.Text = gvSeller.Rows.Count.ToString();
            gvSellerLocked.DataSource = UserDAL.GetUserByRole((int)RoleType.Buyer, (int)UserStatus.Locked);
            gvSellerLocked.DataBind();
            lbSellerLocked.Text = gvSellerLocked.Rows.Count.ToString();
        }

        protected void btnBlock_Click(object sender, EventArgs e)
        {
            Button senderbutton = sender as Button;
            int userID = int.Parse(senderbutton.Attributes["ObjectId"]);
            UserDAL.UpdateStatusUser(userID, (int)UserStatus.Locked);
            LoadUser();
        }
        protected void btnActive_Click(object sender, EventArgs e)
        {
            Button senderbutton = sender as Button;
            int userID = int.Parse(senderbutton.Attributes["ObjectId"]);
            UserDAL.UpdateStatusUser(userID, (int)UserStatus.Active);
            LoadUser();
        }
    }
}